#include "stateboard.h"
#include "ui_stateboard.h"
#include <QtDebug>

StateBoard::StateBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StateBoard)
{
    ui->setupUi(this);
    ui->meter_speed->setProperty("rangeMax", 200);
    ui->meter_speed->setProperty("rangeMin", 0);
    ui->meter_speed->setProperty("scaleMajor", 10);
    ui->meter_speed->setProperty("scaleMinor", 2);
    ui->meter_speed->setProperty("scaleText", ui->meter_speed->property("scaleMajor"));
    ui->meter_speed->setProperty("meterName", "速度\n(Km/s)");
    ui->meter_speed->setProperty("angleRange", 250);
    setSpeed(0);
}

StateBoard::~StateBoard()
{
    delete ui;
}

void StateBoard::carInfo(QStringList info)
{
    if(info.isEmpty())
        return;
    if(info.count()!=7)
        return;

    int speed = QString(info.at(0)).toInt();
    setSpeed(speed);
//    setAcce(info.at(1));
//    setBrake(info.at(2));
//    setDir(info.at(3));
    setDriveMode(info.at(4));
//    setParkingBrake(info.at(5));
//    setTrafficLightFlag(info.at(6));
}

void StateBoard::setDriveMode(QString driveMode)
{
    if(driveMode == "AUTO")
    {
        setControlState(C_AUTO);
    }
    else if(driveMode == "MANU")
    {
        setControlState(C_MANUAL);
    }
}

qreal StateBoard::voltage() const
{
    return m_voltage;
}

bool StateBoard::state4G() const
{
    return m_state4G;
}

int StateBoard::stateGps() const
{
    return m_stateGps;
}

int StateBoard::controlState() const
{
    return m_controlState;
}

int StateBoard::switchState() const
{
    return m_switchState;
}

int StateBoard::lidarState() const
{
    return m_lidarState;
}

int StateBoard::visionState() const
{
    return m_visionState;
}

int StateBoard::speed() const
{
    return m_speed;
}

void StateBoard::setVoltage(qreal voltage)
{
    if(m_voltage == voltage)
        return;
    qDebug()<<"setVoltage"<<voltage;
    m_voltage = voltage;
    ui->mark_v->setText(QString("%1 V").arg(voltage));
}

void StateBoard::setState4G(bool state4G)
{
    if(state4G == m_state4G)
        return;
    qDebug()<<"setState4G"<<state4G;
    m_state4G = state4G;
    ui->mark_network->setVisible(state4G);
}

void StateBoard::setStateGps(int stateGps)
{
    if(stateGps == m_stateGps)
        return;
    qDebug()<<"setStateGps"<<stateGps;
    m_stateGps = (GPS_STATE)stateGps;
    if(stateGps == G_NO)
        ui->mark_gps->hide();
    else
    {
        ui->mark_gps->setVisible(true);
        if(stateGps == G_GPS)
            ui->mark_gps->setText("GPS");
        else if(stateGps == G_RTK)
            ui->mark_gps->setText("RTK");
    }
}

void StateBoard::setControlState(int controlState)
{
    if(m_controlState == controlState)
        return;
    m_controlState = (CONTROL_STATE)controlState;
    if(controlState == C_MANUAL)
        ui->mark_auto->setText("手动");
    else if(controlState == C_AUTO)
        ui->mark_auto->setText("自动");
    else if(controlState == C_REMOTE)
        ui->mark_auto->setText("遥控");
}

void StateBoard::setSwitchState(int switchState)
{
    if(switchState == m_switchState)
        return;
    m_switchState = (EMERGEN_SWITCH_STATE)switchState;
    ui->mark_stop->setVisible(switchState);
}

void StateBoard::setLidarState(int lidarState)
{
    if(lidarState == m_lidarState)
        return;
    m_lidarState = (LIDAR_STATE)lidarState;
    if(lidarState == L_NORMAL)
        ui->mark_lidar->setText("雷达正常");
    else if(lidarState == L_ERROR)
        ui->mark_lidar->setText("雷达异常");
}

void StateBoard::setVisionState(int visionState)
{
    if(visionState == m_visionState)
        return;
    m_visionState = (VISION_STATE)visionState;
    if(visionState == V_NORMAL)
        ui->mark_vision->setText("视觉正常");
    else if(visionState == V_ERROR)
        ui->mark_vision->setText("视觉异常");
}

void StateBoard::setSpeed(int speed)
{
    if(speed == m_speed)
        return;
    m_speed = speed;
    ui->meter_speed->setProperty("value", speed);
}
